/**
 * Created with IntelliJ IDEA.
 * Description:
 * Java集合当中 LinkedList 底层是一个双向链表  这里只是用了一下名字
 * User: GaoBo
 * Date: 2021-04-27
 * Time: 19:20
 */

class ListNode {
    public int val;//值
    public ListNode next;//存储下一个节点的地址  也就是说 这个是一个引用

    public ListNode() {
    }

    public ListNode(int val) {
        this.val = val;
    }

}


public class MyLinkedList {

    public ListNode head;//标识整个链表的头

    //得到单链表的长度
    public int size(){
        int count = 0;
        ListNode cur = this.head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = this.head;
        this.head = node;
    }

    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        //第一次和不是第一次
        if(this.head == null) {
            this.head = node;
        }else {
            ListNode cur = this.head;
            while (cur.next!= null) {
                cur = cur.next;
            }
            //
            cur.next = node;
        }
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data) {

        //1、判断index是否是合法的
        if(index < 0 || index > size()) {
            System.out.println("index位置不合法！");
            return;
        }
        //2.index是0 头插法
        if(index == 0) {
            addFirst(data);
            return;
        }
        //3.index== size()   尾插法
        if(index == size()) {
            addLast(data);
            return;
        }
        //4、cur指向的是一个index-1位置的节点
        ListNode cur = findIndexSubOne(index);
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;
    }

    /**
     * 我们 让一个引用  从头开始 走index-1步
     * @param index
     * @return
     */
    public ListNode findIndexSubOne(int index) {
        int count = 0;
        ListNode cur = this.head;
        while (count != index-1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode cur = this.head;
        while (cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        //cur == null
        return false;
    }

    /**
     * 找要删除的节点key的 前驱节点
     * @param key
     * @return
     */
    public ListNode searchPrev(int key) {
        ListNode prev = this.head;
        while (prev.next != null) {
            if(prev.next.val == key) {
                return prev;
            }
            prev = prev.next;
        }
        return null;//代表没有找到这个节点
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        //0、头结点是你要删除的节点
        if(this.head.val == key) {
            this.head = this.head.next;
            return;
        }
        //1、找要删除的节点的前驱节点
        ListNode prev = searchPrev(key);
        if(prev == null) {
            System.out.println("没有你要删除的节点！");
            return;
        }
        //2、开始删除
        ListNode del = prev.next;
        prev.next = del.next;
    }


    //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode prev = this.head;
        ListNode cur = this.head.next;
        while (cur != null) {
            if(cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        //这个if语句 如果放到前面 必须是循环
        if (this.head.val == key) {
            this.head = this.head.next;
        }
    }

    //怎么测试？？？
    public void clear() {
        //cur:当前需要置空的节点
        ListNode cur = this.head;
        //挨个置空
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = null;
            cur = curNext;
        }
        this.head = null;
    }

    /**
     * 穷举法，最low的方式 先建造一个链表出来，让你来感受一下
     */
    /*public void createList() {
        ListNode listNode1 = new ListNode(12);//12
        ListNode listNode2 = new ListNode(45);
        ListNode listNode3 = new ListNode(6);
        ListNode listNode4 = new ListNode(8);
        ListNode listNode5 = new ListNode(5);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        this.head = listNode1;//head引用  引用了 listNode1引用  引用的对象
    }*/

    /**
     * 遍历链表：
     */
    public void display() {
        ListNode cur = this.head;
        //不能是this.head.next != null  这样写 最后一个节点是不能被打印的
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    /**
     * 从指定位置开始打印链表
     * @param newHead
     */
    public void display2(ListNode newHead) {
        ListNode cur = newHead;
        //不能是this.head.next != null  这样写 最后一个节点是不能被打印的
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
  /* 会导致head的指向发生了改变
   public void display2() {
        while (this.head != null) {
            System.out.print(this.head.val+" ");
            this.head = this.head.next;
        }
        System.out.println();
    }*/

    public ListNode reverseList1() {
        if(this.head == null) {
            return null;
        }
        if(this.head.next == null) {
            //说明只有1个节点
            return this.head;
        }
        ListNode cur = this.head;
        ListNode curNext = cur.next;
        cur.next = null;
        cur = curNext;
        while (cur != null) {
            curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }

    public ListNode reverseList() {
        ListNode prev = null;
        ListNode cur = this.head;
        ListNode newHead = null;
        while (cur != null) {
            ListNode curNext = cur.next;
            if(curNext == null) {
                newHead = cur;
            }
            cur.next = prev;
            prev = cur;
            cur = curNext;
        }
        return newHead;
    }

    public ListNode middleNode() {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        //while (fast != null ||  fast.next != null) {
        return slow;
    }


    public ListNode findKthToTail(int k) {
        if(k <= 0 || head == null) {
            return null;
        }
        ListNode fast = this.head;
        ListNode slow = this.head;
        while (k-1 != 0) {
            if(fast.next != null) {
                fast = fast.next;
                k--;
            }else {
                System.out.println("k的值过大！");
                return null;
            }
        }
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }


    public ListNode partition(int x) {

        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;

        while (head != null) {
            if(head.val < x) {
                if(bs == null) {
                    //第一次插入
                    bs = head;
                    be = head;
                }else {
                    //不是第一次插入
                    be.next = head;
                    be = be.next;
                }
            } else {
                if(as == null) {
                    //第一次插入
                    as = head;
                    ae = head;
                }else {
                    //不是第一次插入
                    ae.next = head;
                    ae = ae.next;
                }
            }
            head = head.next;
        }
        //如果第一个线段没有数据，那么直接返回as
        if(bs == null) {
            return as;
        }
        be.next = as;
        //预防最后一个节点的next值，不是null.
        if(as != null) {
            ae.next = null;
        }
        return bs;
    }


    public ListNode deleteDuplication() {
        ListNode cur = this.head;
        ListNode newHead = new ListNode();
        ListNode tmp = newHead;
        while (cur != null) {
            if(cur.next != null && cur.val == cur.next.val) {
                while (cur.next != null && cur.val == cur.next.val) {
                    cur = cur.next;
                }
                cur = cur.next;//多走一步！
            }else {
                tmp.next = cur;
                tmp = tmp.next;
                cur = cur.next;
            }
        }
        tmp.next = null;//预防重复的节点在最后！
        return newHead.next;
    }

    public boolean chkPalindrome() {
        //1、找中间节点
        ListNode fast = this.head;
        ListNode slow = this.head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        //2、反转
        ListNode cur = slow.next;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = slow;
            slow  = cur;
            cur = curNext;
        }
        //3、一个从前往后  一个从后往前 知道相遇 就是回文的
        while (head != slow) {
            if(head.val != slow.val) {
                return false;
            }
            if(head.next == slow) {
                //偶数情况下！
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }
    public boolean hasCycle1() {

        ListNode fast = head;
        ListNode slow = head;

        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;

            if(fast == slow) {
                return true;
            }

        }
        return false;
    }

    public void createLoop() {
        ListNode cur = this.head;
        while (cur.next != null) {
            cur = cur.next;
        }
        //cur是尾巴节点
        cur.next = this.head.next;
    }



    public boolean hasCycle() {
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) {
                break;
            }
        }
        if(fast == null || fast.next == null) {
            return false;
        }
        return true;
    }

    public ListNode detectCycle() {
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) {
                break;
            }
        }
        if(fast == null || fast.next == null) {
            return null;
        }
        slow = head;
        while(fast != slow) {
            slow = slow.next;
            fast = fast.next;
        }
        return slow;
    }

}











